package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.vo.StartAuditVO;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;


    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        ServiceItem newItem = new ServiceItem();
        newItem.setInfo(serviceItem.getInfo());
        newItem.setCarPackage(serviceItem.getCarPackage());
        newItem.setServiceCatalog(serviceItem.getServiceCatalog());
        newItem.setOriginalPrice(serviceItem.getOriginalPrice());
        newItem.setDiscountPrice(serviceItem.getDiscountPrice());
        newItem.setName(serviceItem.getName());


        //判断前台传递套餐状态
        if(serviceItem.getCarPackage().equals(ServiceItem.CARPACKAGE_YES)){
            newItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }else{
            newItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        // 设置创建时间
        newItem.setCreateTime(new Date());
        serviceItemMapper.insert(newItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem oldItem = this.get(serviceItem.getId());
        // 如果是审核中 或者是 上架状态抛出异常
        if(oldItem.getAuditStatus().equals(ServiceItem.AUDITSTATUS_AUDITING)
                || oldItem.getSaleStatus().equals(ServiceItem.SALESTATUS_ON)){
            throw new BusinessException("非法操作");
        }

        serviceItem.setAuditStatus(oldItem.getAuditStatus());

        // 判断服务单项如果是套餐 ,并且是审核通过状态 , 把审核状态修改成初始化状态
        if(oldItem.getCarPackage().equals(ServiceItem.CARPACKAGE_YES)
                && oldItem.getAuditStatus().equals(ServiceItem.AUDITSTATUS_APPROVED)){
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        serviceItemMapper.updateByPrimaryKey(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    @Override
    public void saleOn(Long id) {
        // 1 查询数据
        ServiceItem serviceItem = this.get(id);
        // 2 判断状态是否为下架状态
        if(!ServiceItem.SALESTATUS_OFF.equals(serviceItem.getSaleStatus())){
            throw new BusinessException("非法操作");
        }
        // 3 判断数据如果是套餐但是审核状态为非审核通过
        if(ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())
                && !ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())){
            throw new BusinessException("非法操作");
        }
        // 4 修改状态
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_ON);
    }

    @Override
    public void saleOff(Long id) {
        // 1 查询数据
        ServiceItem serviceItem = this.get(id);
        // 2 判断是否处于上架状态
        if(!ServiceItem.SALESTATUS_ON.equals(serviceItem.getSaleStatus())){
            throw new BusinessException("非法操作");
        }
        // 3 修改上架状态
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_OFF);

    }

    @Override
    @Transactional
    public void startAudit(StartAuditVO vo) {
        //1 业务逻辑校验 校验是否是套餐 以及审核状态是否是审核中或者审核通过
        ServiceItem serviceItem = get(vo.getId());
        if(serviceItem.getCarPackage().equals(ServiceItem.CARPACKAGE_NO)){
            throw new BusinessException("非法操作");
        }

        if(serviceItem.getAuditStatus().equals(ServiceItem.AUDITSTATUS_AUDITING)
                || serviceItem.getAuditStatus().equals(ServiceItem.AUDITSTATUS_APPROVED)){
            throw new BusinessException("非法操作");
        }
        // 把信息保存到CarPackageAudit当中
        CarPackageAudit carPackageAudit = new CarPackageAudit();
        carPackageAudit.setServiceItemId(serviceItem.getId());
        carPackageAudit.setServiceItemInfo(serviceItem.getInfo());
        carPackageAudit.setServiceItemPrice(serviceItem.getDiscountPrice());
        carPackageAudit.setCreateTime(new Date());
        carPackageAudit.setInfo(vo.getInfo());
        carPackageAudit.setAuditorId(vo.getDirector());
        carPackageAudit.setBpmnInfoId(vo.getBpmnInfoId());
        carPackageAudit.setCreator(ShiroUtils.getUser().getUserName());
        // 保存到数据库中
        carPackageAuditMapper.insert(carPackageAudit);
        // 根据id查询bpmnInfo 信息
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(vo.getBpmnInfoId());
        // 设置流程变量
        Map<String,Object> map = new HashMap<>();
        map.put("director",vo.getDirector());
        if(vo.getFinance() != null){
            map.put("finance",vo.getFinance());
        }
        map.put("disCountPrice",serviceItem.getDiscountPrice().longValue());
        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey(bpmnInfo.getActProcessKey(), carPackageAudit.getId().toString(), map);
        // 设置流程实例id
        carPackageAudit.setInstanceId(processInstance.getId());
        carPackageAuditMapper.updateByPrimaryKey(carPackageAudit);
        // 修改养修父单项状态
        serviceItemMapper.changeAuditStatus(vo.getId(),ServiceItem.AUDITSTATUS_AUDITING);



    }


}
